<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/03/0003
 * Time: 16:43
 */

namespace app\sports_admin\service;


use app\exception\SchoolException;
use think\Config;
use think\Request;
use app\sports_admin\model\School as SchoolModel;
class School
{
    /**
     * 添加学校
     * @param $schoolName 学校名称
     * @param $phoneName 联系人
     * @param $phone 联系方式(登录账号)
     * @param $password 登录密码
     * @param $url 学校官网
     * @param $email 电子邮件
     * @param $address 地址
     * @param $province 省
     * @param $city 市
     * @param $district 县
    */
    public static function schoolInsert()
    {
        $params = Request::instance()->only(['school_name','login','phone_name','phone','url','email','address','password','end','start']);
        if(SchoolModel::where('phone',$params['phone'])->find()){
            throw new SchoolException([
                'msg'=>'手机号已存在',
                'errorCode'=>400
            ]);
        }
        if(SchoolModel::Where('login',$params['login'])->find()){
            throw new SchoolException([
                'msg'=>'账号已存在',
                'errorCode'=>400
            ]);
        }
        $city = $_POST['province'];
        $params['province'] = intval(isset($city[0]))>0 ? intval($city[0]) : '';
        $params['city'] = intval(isset($city[1]))>0 ? intval($city[1]) : '';
        $params['district'] = intval(isset($city[2]))>0?intval($city[2]) : '';
        $key  = Config::get('schoolPwd');
        $params['password'] = authcode(trim($params['password']),'ENCODE',$key['key'],0);
        $params['end'] = strtotime($params['end']);
        $params['start'] = strtotime($params['start']);
        $type = $_POST['type'];
        $params['category'] = implode(',',$type);
        $schoolId = SchoolModel::schoolInsert($params);
        if(!$schoolId){
            throw new SchoolException([
                'msg'=>'学校信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>200,'msg'=>'添加成功']);
    }
    /**
     * 添加学校
     * @param $phone 联系方式(登录账号)
     * @param $phoneName 联系人
     */
    public static function schoolUpdate()
    {
        $params = Request::instance()->only(['school_name','login','phone_name','phone','r_phone','url','email','address','end','start']);
        if($params['r_phone'] != $params['phone']){
            if(SchoolModel::where('phone',$params['phone'])->find()){
                throw new SchoolException([
                    'msg'=>'手机号已存在',
                    'errorCode'=>400
                ]);
            }
        }
        unset($params['r_phone']);
        $id = input('post.id');
        $city = $_POST['province'];
        if(count($city) != 1){
            $params['province'] = intval(isset($city[0]))>0 ? intval($city[0]) : '';
            $params['city'] = intval(isset($city[1]))>0 ? intval($city[1]) : '';
            $params['district'] = intval(isset($city[2]))>0 ? intval($city[2]) : '';
        }
        $params['end'] = strtotime($params['end']);
        $params['start'] = strtotime($params['start']);
        $type = $_POST['type'];
        $params['category'] = implode(',',$type);
//        var_dump($params);
//        exit();
        $bool = (new SchoolModel())->save($params,['school_id'=>$id]);
        if(!$bool){
            throw new SchoolException([
                'msg'=>'学校信息修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
    /**
     * 密码重置功能
     * @param $id 学校信息id
    */
    public static function resetPass($id)
    {
        $schoolPwd = Config::get('schoolPwd');
        //$schoolInfo = SchoolModel::where('school_id',$id)->find();
        $newPass = authcode('111111','DECODE',$schoolPwd['key'],0);
        $bool = (new SchoolModel())->save(['password'=>$newPass],['school_id'=>$id]);
        if(!$bool){
            throw new SchoolException([
                'msg'=>'密码重置失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'重置成功']);
    }
}